package com.yjy.fuyao.sta.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yjy.fuyao.sta.client.StuClient;
import com.yjy.fuyao.sta.client.UcenterClient;
import com.yjy.fuyao.sta.entity.StatisticsDaily;
import com.yjy.fuyao.sta.mapper.StatisticsDailyMapper;
import com.yjy.fuyao.sta.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author yjy
 * @since 2021-08-27
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Resource
    private UcenterClient ucenterClient;

    @Resource
    private StuClient stuClient;

    @Override
    public void createStatisticsByDay(String day) {

        //获取统计信息
        Integer registerNum = (Integer) ucenterClient.registerCount(day).getData().get("countRegister");
        Integer courseNum = (Integer) stuClient.newCourseCount(day).getData().get("countNewCourse");

        //创建统计对象
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(registerNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(day);

        StatisticsDaily statisticsDaily = baseMapper.selectOne(new LambdaQueryWrapper<StatisticsDaily>().eq(StatisticsDaily::getDateCalculated, day));
        if (statisticsDaily == null) {
            baseMapper.insert(daily);
        } else {
            baseMapper.update(daily,new LambdaUpdateWrapper<StatisticsDaily>()
                    .eq(StatisticsDaily::getDateCalculated,daily.getDateCalculated())
                    .set(StatisticsDaily::getCourseNum,daily.getCourseNum())
                    .set(StatisticsDaily::getRegisterNum,daily.getRegisterNum()));
        }
    }

    @Override
    public Map<String, Object> getChartData(String begin, String end, String type) {

        QueryWrapper<StatisticsDaily> dayQueryWrapper = new QueryWrapper<>();
        dayQueryWrapper.select(type, "date_calculated");
        dayQueryWrapper.between("date_calculated", begin, end);

        List<StatisticsDaily> dayList = baseMapper.selectList(dayQueryWrapper);

        Map<String, Object> map = new HashMap<>();
        List<Integer> dataList = new ArrayList<Integer>();
        List<String> dateList = new ArrayList<String>();
        map.put("dataList", dataList);
        map.put("dateList", dateList);


        for (int i = 0; i < dayList.size(); i++) {
            StatisticsDaily daily = dayList.get(i);
            dateList.add(daily.getDateCalculated());
            switch (type) {
                case "register_num":
                    dataList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    dataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    dataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        return map;
    }

    @Override
    public boolean AddRegisterNum() {
        String day = LocalDateTime.now().toLocalDate().toString();
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setRegisterNum(1);
        statisticsDaily.setCourseNum(0);
        statisticsDaily.setLoginNum(0);
        statisticsDaily.setVideoViewNum(0);
        try {
            this.save(statisticsDaily);
        } catch (Exception e) {
            e.printStackTrace();
            baseMapper.AddRegisterNum(day);
        }
        return true;
    }

    @Override
    public boolean AddLoginNum() {
        String day = LocalDateTime.now().toLocalDate().toString();
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setRegisterNum(0);
        statisticsDaily.setCourseNum(0);
        statisticsDaily.setLoginNum(1);
        statisticsDaily.setVideoViewNum(0);
        try {
            this.save(statisticsDaily);
        } catch (Exception e) {
            e.printStackTrace();
            baseMapper.AddLoginNum(day);
        }
        return true;
    }

    @Override
    public boolean AddCourseNum() {
        String day = LocalDateTime.now().toLocalDate().toString();
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setRegisterNum(0);
        statisticsDaily.setCourseNum(1);
        statisticsDaily.setLoginNum(0);
        statisticsDaily.setVideoViewNum(0);
        try {
            this.save(statisticsDaily);
        } catch (Exception e) {
            e.printStackTrace();
            baseMapper.AddCourseNum(day);
        }
        return true;
    }

    @Override
    public boolean AddVideoViewNum() {
        String day = LocalDateTime.now().toLocalDate().toString();
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setRegisterNum(0);
        statisticsDaily.setCourseNum(0);
        statisticsDaily.setLoginNum(0);
        statisticsDaily.setVideoViewNum(1);
        try {
            this.save(statisticsDaily);
        } catch (DuplicateKeyException e) {
            e.printStackTrace();
            baseMapper.AddVideoViewNum(day);
        }
        return true;
    }

}
